//
//  Test4ViewController.swift
//  JKRegex
//
//  Created by 王冲 on 2018/2/7.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

class Test4ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
      
        self.view.backgroundColor = UIColor.white;
        
        test()
    }
    
    func test2() {
        
        let str = "@jack12:【动物尖叫合辑】#肥猪流#猫头鹰这么https://www.520it.com 尖叫[偷笑]、@南哥: 老鼠这么尖叫、兔子这么尖叫[吃惊]、@江哥: 莫名奇#小笼包#妙的笑到最后[挖鼻屎]！~ http://t.cn/zYBuKZ8"
        
        // 1.创建一个正则表达式对象
        do{
            let dataDetector = try NSDataDetector(types: NSTextCheckingTypes(NSTextCheckingResult.CheckingType.link.rawValue))
            let res =  dataDetector.matches(in: str, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, str.count))
            // 4取出结果
            for checkingRes in res
            {
                print((str as NSString).substring(with: checkingRes.range))
            }
        }catch
        {
            print(error)
        }
    }
    
    func test()
    {
        let str = "@joanking:【周杰伦的歌曲】#大眼睛#小猫咪这么尖叫[偷笑]、@老北: 蝉叫、狼这么尖叫[吃惊]、@乐不思蜀:达芬奇#烧饼#妙的笑到最后[挖鼻屎]！~ http://www.baidu.com"
        do{
            // 1.创建规则
            let pattern1 = "\\[.*?\\]"
            let pattern2 = "@.*?:"
            let pattern3 = "#.*?#"
            
            // 多个规则之间使用 | 符号连接
            let pattern = pattern1 + "|" + pattern2 + "|" + pattern3
            
            // 2.创建正则表达式对象
            let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.caseInsensitive)
            // 3.开始匹配
            let res = regex.matches(in: str, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, str.count))
            // 4取出结果
            for checkingRes in res
            {
                // print(checkingRes.range)
                print((str as NSString).substring(with: checkingRes.range))
            }
        }catch
        {
            print(error)
        }
        
    }
}


